package com.uenpay.im.ui.old.activity.company;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-3-5 13:37:35
 * @copyRight http://www.qiyunxin.com
 * @function 所有公司列表
 */
public class AllCompanyListActivity extends Activity {

	private CompanyHandle mCompanyHandle = new CompanyHandle();
	private CompanyAdapter mCompanyAdapter = null;
	private ListView listView;
	private boolean choose_company = false;
	/**
	 * 0 当前用户不在的企业 myexist:1 当前用户所在的企业 myexist:空 所有的企业
	 */
	private int myexist = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_list_layout);
		if (getIntent() != null && getIntent().hasExtra("choose_company")) {
			choose_company = getIntent().getBooleanExtra("choose_company",
					false);
		}

		if (getIntent() != null && getIntent().hasExtra("myexist")) {
			myexist = getIntent().getIntExtra("myexist", 0);
		}
		initView();
		initListener();
		getCompanyContacts();
	}

	private void initView() {
		listView = (ListView) findViewById(R.id.listview);
		if(choose_company){
			((TextView)findViewById(R.id.title_textview)).setText(R.string.choose_company);
		}
	}

	private void initListener() {
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				CompanyEntity mCompanyEntity = (CompanyEntity) arg0
						.getAdapter().getItem(arg2);
				Intent intent = new Intent();
				if (mCompanyEntity != null) {
					if (choose_company) {
						intent.putExtra("mCompanyEntity", mCompanyEntity);
						setResult(RESULT_OK, intent);
						finish();
					} else {
						Bundle bundle = new Bundle();
						bundle.putInt("entid",  mCompanyEntity.entid);
						bundle.putInt("departid",  0);
						ActivityHandle.startActivity(AllCompanyListActivity.this,
								OnlyCompanyDeparListActivity.class, bundle, 100);
					}
				}

			}
		});
	}

	private void getCompanyContacts() {
		int type = 2;
		if (choose_company || myexist == 1) {
			type = 1;
		}
		mCompanyHandle.getMyCompanyList(type, myexist, new CompanyHandle.IGetMyCompanyListener() {

			@Override
			public void onGetCompanyResult(int status, String msg,
					ArrayList<CompanyEntity> arrayList) {
				boolean is_choose_company = false;
				if (choose_company || myexist == 1) {
					is_choose_company = true;
				}
				if (arrayList != null && arrayList.size() > 0) {
					mCompanyAdapter = new CompanyAdapter(
							AllCompanyListActivity.this, arrayList,
							is_choose_company);
					listView.setAdapter(mCompanyAdapter);
				}
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 100 && resultCode == RESULT_OK) {
			String departName = data.getStringExtra("departName");
			int departId = data.getIntExtra("departId", 0);
			Intent intent = new Intent();
			intent.putExtra("departName", departName);
			intent.putExtra("departId", departId);
			setResult(RESULT_OK, intent);
			finish();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}
}
